﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PDA.入库管理
{
    /// <summary>
    /// 入库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PDA")]
    public class ReceivingOrderDetailsController : BaseController
    {
        private readonly IPalletService palletService;
        private readonly IReceivingOrderDetailService receivingOrderDetailService;
        private readonly IPickingOrderRepository pickingOrderRepository;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="receivingOrderDetailService"></param>
        /// <param name="palletService"></param>
        public ReceivingOrderDetailsController(IWebHostEnvironment environment, IReceivingOrderDetailService receivingOrderDetailService, IPalletService palletService, IPickingOrderRepository pickingOrderRepository) : base(environment)
        {
            this.receivingOrderDetailService = receivingOrderDetailService;
            this.pickingOrderRepository = pickingOrderRepository;
            this.palletService = palletService;
        }

        /// <summary>
        /// PDA收货入库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BuildCreateStackingStorage(ReceivingBillOrderDto m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message = "";
            m.NowUserId = base.BaseGetNowUserId();
            m.NowUserName = base.BaseGetNowUserName();
            var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.Status != 5);
            if (pickobj != null)
                return Ok(BaseResult.Result(false, "保存失败！有出库任务正在执行！", "500"));
            bool IsOK = receivingOrderDetailService.AddReceivingOrderDetail(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "保存成功！分配的仓位为：" + message, "200"));
            }
            return Ok(BaseResult.Result(false, "保存失败！" + message, "500"));
        }

        /// <summary>
        /// 判断托盘是否可用
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult PalletVerify(string PalletCode)
        {
            if (string.IsNullOrEmpty(PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            string message = "";
            var obj = palletService.PalletVerify(PalletCode, out message);
            if (obj)
            {
                return Ok(BaseResult.Result(true, "", "200", 0));
            }
            return Ok(BaseResult.Result(false, message, "500", 0));
        }

        /// <summary>
        /// 物料入库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartReceivingOrderDetail(StartInventoryDTO m)
        {
            if (string.IsNullOrEmpty(m.PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            if (string.IsNullOrEmpty(m.ActualLocation))
            {
                return Ok(BaseResult.Result("400", "仓位信息有误！"));
            }
            string message = "";
            m.NowUserId = base.BaseGetNowUserId();
            m.NowUserName = base.BaseGetNowUserName();
            bool result = receivingOrderDetailService.StartReceivingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "操作成功", "200"));
            }
            return Ok(BaseResult.Result(false, "操作失败" + message, "500"));
        }


        /// <summary>
        /// 入库查询单据
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingRoderData(string PalletCode)
        {
            if (string.IsNullOrEmpty(PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            var obj = receivingOrderDetailService.GetReceivingRoderData(PalletCode, out string message);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
        }

        /// <summary>
        /// 拣选入库时获取库存信息
        /// </summary>
        /// <param name="palletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPickReceivingsInventoryDto(string palletCode)
        {
            if (string.IsNullOrEmpty(palletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            var obj = receivingOrderDetailService.GetPickReceivingsInventoryDto(palletCode, out string message);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
        }

        /// <summary>
        /// 拣选入库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartPickReceivingOrderDetail(PickReceivingsDto m)
        {
            string message = "";
            var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.Status != 5);
            if (pickobj != null)
                return Ok(BaseResult.Result(false, "保存失败！有出库任务正在执行！", "500"));
            bool result = receivingOrderDetailService.StartPickReceivingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "操作成功", "200"));
            }
            return Ok(BaseResult.Result(false, "操作失败" + message, "500"));
        }
    }
}
